local love = require("love")
local hero = {}
local QUARD_WIDTH, QUARD_HEIGHT = 669, 569

local function newAnimation(image, width, height, duration)
    local animation = {}
    animation.spritesheet = image
    animation.quards = {}

    for y = 0, image:getHeight() - height, height do
        for x = 0, image:getWidth() - width, width do
            table.insert(animation.quards, love.graphics.newQuad(x, y, width, height, image:getDimensions()))
        end
    end

    animation.duration = duration or 1
    animation.current_time = 0

    return animation
end

function love.load()
    hero.animation = {}
    hero.animation["run"] = newAnimation(love.graphics.newImage("run.png"), QUARD_WIDTH, QUARD_HEIGHT, 0.7)
    hero.animation["jump"] = newAnimation(love.graphics.newImage("jump.png"), QUARD_WIDTH, QUARD_HEIGHT, 0.7)
    hero.x = 0
    hero.y = love.graphics.getHeight()
    hero.vertical_direction = "middle"
    hero.horizontal_direction = "right"
    hero.idle = true
    hero.speed = 10
end

function love.update(dt)
    if not hero.idle then
        hero.animation["run"].current_time = hero.animation["run"].current_time + dt
        if hero.animation["run"].current_time >= hero.animation["run"].duration then
            hero.animation["run"].current_time = hero.animation["run"].current_time - hero.animation["run"].duration
        end

        hero.animation["jump"].current_time = hero.animation["jump"].current_time + dt
        if hero.animation["jump"].current_time >= hero.animation["jump"].duration then
            hero.animation["jump"].current_time = hero.animation["jump"].current_time - hero.animation["jump"].duration
        end
    end
end

function love.draw()
    if love.keyboard.isDown("d") then
        hero.idle = false
        hero.horizontal_direction = "right"
        hero.x = hero.x + hero.speed
    elseif love.keyboard.isDown("a") then
        hero.idle = false
        hero.horizontal_direction = "left"
        hero.x = hero.x - hero.speed
    elseif love.keyboard.isDown("w") then
        hero.idle = false
        hero.vertical_direction = "up"
    else
        hero.idle = true
        hero.vertical_direction = "middle"
        hero.animation["run"].current_time = 0
        hero.animation["jump"].current_time = 0
    end

    love.graphics.scale(0.3)
    if hero.vertical_direction == "up" then
        local sprite_num = math.floor(hero.animation["jump"].current_time / hero.animation["jump"].duration * #hero.animation["jump"].quards) + 1
        if hero.horizontal_direction == "right" then
            love.graphics.draw(hero.animation["jump"].spritesheet, hero.animation["jump"].quards[sprite_num], hero.x, hero.y)
        else
            love.graphics.draw(hero.animation["jump"].spritesheet, hero.animation["jump"].quards[sprite_num], hero.x, hero.y, 0, -1, 1, QUARD_WIDTH, 0)
        end
    else
        local sprite_num = math.floor(hero.animation["run"].current_time / hero.animation["run"].duration * #hero.animation["run"].quards) + 1
        if hero.horizontal_direction == "right" then
            love.graphics.draw(hero.animation["run"].spritesheet, hero.animation["run"].quards[sprite_num], hero.x, hero.y)
        else
            love.graphics.draw(hero.animation["run"].spritesheet, hero.animation["run"].quards[sprite_num], hero.x, hero.y, 0, -1, 1, QUARD_WIDTH, 0)
        end
    end
end
